import requests
from lxml import etree
import csv

"""
2024-04-17 一个客户要帮忙看的，很简单，不过报价200没接到
"""
if __name__ == '__main__':

    csv_file = open('可视化年报.csv', 'w', encoding='gbk', newline='')
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(['name', 'time'])

    for i in range(0,5):
        url = 'https://dataviz.cfbond.com/zh-hans/vfr-zzb-search?date_type=All&stock_name_and_code=&page={}'
        headers = {
            'Cookie': 'device_id=7378f1460e605d6bbb9d845df28a042f; __utma=1.146408891.1483277965.1507822021.1507904229.10; __utmz=1.1496161318.8.4.utmcsr=xueqiu.com|utmccn=(referral)|utmcmd=referral|utmcct=/; remember=1; remember.sig=K4F3faYzmVuqC0iXIERCQf55g2Y; xq_a_token=7c56ad53fa7fbf66f33a52ae7ce3fa82e0affea4; xq_a_token.sig=oPtXoO1FnzRynhx-pSwvF9YvvK0; xq_r_token=c28549d2af9dd8bbf353559f49868e46425a254b; xq_r_token.sig=WclLch4ntYyswevQw1-mjVCs_f0; xq_is_login=1; xq_is_login.sig=J3LxgPVPUzbBg3Kee_PquUfih7Q; u=3094390085; u.sig=EsGua0pSN_rwwiOGMVwjjbUXF_s; aliyungf_tc=AQAAAI1aplruAwEAh95b0/CSScREtven; Hm_lvt_1db88642e346389874251b5a1eded6e3=1516710527,1516710801,1516888695; Hm_lvt_9d483e9e48ba1faa0dfceaf6333de846=1516711294,1516888696; s=gk1prcz9cq; bid=269d98283aafb9910fb4cab2ed6e57c8_jcxf0jb8; snbim_minify=true; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1516889894; Hm_lpvt_9d483e9e48ba1faa0dfceaf6333de846=1516889894',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
        }
        real_url = url.format(i+1)
        print(real_url)
        html_content = requests.get(real_url, headers=headers).content
        # 使用lxml解析HTML内容
        tree = etree.HTML(html_content)

        # 使用XPath定位标题和更新时间
        titles = tree.xpath("//div[@class='view-content']/table/tbody/tr")

        for j in titles:
            list = []
            try:
                name = j.xpath('./td[1]/a/text()')[0]
                time = j.xpath('./td[2]/text()')[0]
            except:
                print(str(i) + ' 遇到异常')
                pass

            list.append(name.rstrip())  # 去掉空格
            list.append(time.rstrip())
            csv_writer.writerow(list)

            print(str(list), '第', i+1, '页')



